// pages/ed/ed.js
Page({
  data: {
    gender: '',
    avatarUrl: '',
    nickName: '',
    idNumber: '', // 身份证号
    phone: '', // 手机号
    id: '',
    token: '' // 存储 Token
  },

  onLoad: function () {
    // 页面加载时从本地存储获取用户信息
    const storedUserInfo = wx.getStorageSync('userInfo');
    if (storedUserInfo) {
      this.setData({
        nickName: storedUserInfo.nickName,
        idNumber: storedUserInfo.idNumber,
        phone: storedUserInfo.phone,
        gender: storedUserInfo.gender
      });
    } else {
      // 如果本地没有存储userInfo，设置默认值
      this.setData({
        nickName: '',
        idNumber: '',
        phone: '',
        gender: ''
      });
    }
  },

  onShow: function () {
    // 每次页面显示时重新从本地存储获取数据
    const storedUserInfo = wx.getStorageSync('userInfo');
    const token = wx.getStorageSync('token');
    if (storedUserInfo) {
      this.setData({
        avatarUrl: storedUserInfo.avatarUrl,
        nickName: storedUserInfo.nickName,
        idNumber: storedUserInfo.idNumber,
        phone: storedUserInfo.phone,
        gender: storedUserInfo.gender,
        id: storedUserInfo.id,
      });
    } else {
      // 如果本地没有存储userInfo，尝试单独获取每个字段
      const avatarUrl = wx.getStorageSync('avatarUrl');
      const nickName = wx.getStorageSync('nickName');
      const id = wx.getStorageSync('id');
      this.setData({
        avatarUrl,
        nickName,
        id,
        token
      });
    }
  },

  onNameInput: function (e) {
    this.setData({
      nickName: e.detail.value
    });
  },

  onIdNumberInput: function (e) {
    this.setData({
      idNumber: e.detail.value
    });
  },

  onPhoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    });
  },

  onGenderChange: function (e) {
    this.setData({
      gender: e.detail.value // 更新性别数据
    });
  },

  chooseAvatar: function () {
    // 调用选择头像的相关函数
    wx.chooseImage({
      success: (res) => {
        // 设置用户头像
        const avatarUrl = res.tempFilePaths[0];
        this.setData({
          avatarUrl
        });
        // 可以在这里上传图片到服务器，并更新本地存储
      }
    });
  },

  saveProfile: function () {
    // 构建要发送给服务器的用户信息对象
    const userInfo = {
      avatar: this.data.avatarUrl,
      name: this.data.nickName,
      idNumber: this.data.idNumber,
      phone: this.data.phone,
      sex: this.data.gender,
      id: this.data.id // 包含用户ID以供服务器识别
    };
    const token = wx.getStorageSync('token'); // 获取 Token
    console.log(token)
    const app = getApp();
    // 发送请求到服务器更新用户信息
    wx.request({
      url: `${app.globalData.baseUrl}/user/update-user-info`,
      method: 'POST',
      data: userInfo,
      header: {
        'authentication': token
      },
      success: (res) => {
        if (res.statusCode === 200 && res.data.code === 1) {
          this.setData({
            avatarUrl: res.data.data.avatar,
            nickName: res.data.data.name,
            idNumber: res.data.data.idNumber,
            phone: res.data.data.phone,
            gender: res.data.data.sex,
            id: res.data.data.id
          });
          // 更新本地存储的用户信息
          wx.setStorageSync('userInfo', {
            avatarUrl: res.data.data.avatar,
            nickName: res.data.data.name,
            idNumber: res.data.data.idNumber,
            phone: res.data.data.phone,
            gender: res.data.data.sex,
            id: res.data.data.id
          });
          // 提示用户信息已保存
          wx.showToast({
            title: '信息已更新',
            icon: 'success',
            duration: 2000
          });

          // 返回到个人中心页面
          wx.switchTab({
            url: '/pages/contact/contact',
          });
        } else if (res.statusCode === 401) {
          // Token 过期，需要重新登录
          wx.showToast({
            title: '登录已过期，请重新登录',
            icon: 'none',
            duration: 2000
          });
          wx.removeStorageSync('token');
          // 重定向到登录页面
          wx.navigateTo({
            url: '/pages/index/index'
          });
        } else {
          // 处理服务器返回的其他错误
          wx.showToast({
            title: '更新失败，请重试',
            icon: 'none',
            duration: 2000
          });
        }
      },
      fail: (err) => {
        // 处理请求失败的情况
        console.error(err);
        wx.showToast({
          title: '请求失败，请检查网络',
          icon: 'none',
          duration: 2000
        });
      }
    });
  }
});